import request from '@/utils/request'

// 스테이킹 상품 관련 API
export interface StakingProduct {
  id: number
  name: string
  symbol: string
  iconUrl?: string
  contractAddress?: string
  dailyRewardRate: number
  minStakeAmount: number
  maxStakeAmount?: number
  stakingPeriodDays?: number
  isActive: boolean
  description?: string
  applicationCount?: number
  createdAt: string
  updatedAt: string
}

export interface StakingProductForm {
  name: string
  symbol: string
  iconUrl?: string
  contractAddress?: string
  dailyRewardRate: number
  minStakeAmount: number
  maxStakeAmount?: number
  stakingPeriodDays?: number
  description?: string
}

export interface StakingApplication {
  id: number
  userId: number
  productId: number
  amount: number
  dailyRewardRate: number
  status: 'PENDING' | 'APPROVED' | 'ACTIVE' | 'COMPLETED' | 'CANCELLED' | 'REJECTED'
  startDate?: string
  endDate?: string
  totalRewards: number
  adminNote?: string
  createdAt: string
  updatedAt: string
  user: {
    id: number
    name: string
    idNumber: string
    mobileNumber: string
  }
  product: {
    id: number
    name: string
    symbol: string
    iconUrl?: string
  }
}

export interface StakingHistory {
  id: number
  userId: number
  type: 'STAKE' | 'UNSTAKE' | 'WITHDRAW_REWARD'
  amount: number
  status: 'PENDING' | 'COMPLETED' | 'FAILED' | 'CANCELLED'
  txHash?: string
  createdAt: string
  user: {
    id: number
    name: string
    idNumber: string
    mobileNumber: string
  }
}

export interface StakingReward {
  id: number
  userId: number
  amount: number
  txHash?: string
  createdAt: string
  user: {
    id: number
    name: string
    idNumber: string
    mobileNumber: string
  }
}

// 스테이킹 상품 관리
export const getStakingProducts = (params: {
  page?: number
  limit?: number
  search?: string
  status?: string
}) => {
  return request.get('/api/admin/staking/products', { params })
}

export const createStakingProduct = (data: StakingProductForm) => {
  return request.post('/api/admin/staking/products', data)
}

export const getStakingProduct = (id: number) => {
  return request.get(`/api/admin/staking/products/${id}`)
}

export const updateStakingProduct = (id: number, data: Partial<StakingProductForm & { isActive: boolean }>) => {
  return request.put(`/api/admin/staking/products/${id}`, data)
}

export const deleteStakingProduct = (id: number) => {
  return request.delete(`/api/admin/staking/products/${id}`)
}

// 스테이킹 신청 관리 - 已废弃，请使用 getStakingList
// export const getStakingApplications = (params: {
//   page?: number
//   limit?: number
//   status?: string
//   userId?: string
//   productId?: string
// }) => {
//   return request.get('/api/admin/staking/applications', { params })
// }

// export const updateApplicationStatus = (id: number, data: { status: string; adminNote?: string }) => {
//   return request.put(`/api/admin/staking/applications/${id}/status`, data)
// }

// 新API：获取用户质押列表（替代getStakingApplications）
export const getStakingList = (params: {
  page?: number
  limit?: number
  search?: string
}) => {
  return request.get('/api/admin/staking/staking-list', { params })
}

// 스테이킹 이력
export const getStakingHistory = (params: {
  page?: number
  limit?: number
  userId?: string
  type?: string
}) => {
  return request.get('/api/admin/staking/history', { params })
}

// 스테이킹 보상
export const getStakingRewards = (params: {
  page?: number
  limit?: number
  userId?: string
}) => {
  return request.get('/api/admin/staking/rewards', { params })
}